﻿using MRMDS.Module.Response;

namespace MRMDS.Module
{
    public class Viewer
    {
        private List<Actor> watchSets;
        public Viewer(string uuid)
        {
            UUID = uuid;
            watchSets = new List<Actor>();
        }
        public string UUID { get; set; }
        public string[] WatchSet
        {
            get
            {
                string[] sets = new string[watchSets.Count];
                for (int i = 0; i < watchSets.Count; i++)
                {
                    sets[i] = watchSets[i].UUID;
                }
                return sets;
            }
            set
            {
                watchSets.Clear();
                HashSet<string> sets = new HashSet<string>();
                foreach (var uuid in value)
                {
                    sets.Add(uuid);
                }
                foreach (var uuid in sets)
                {
                    if (!GlobalManager.Instance.UUID2Actor.TryGetValue(uuid, out var instance))
                    {
                        continue;
                    }
                    watchSets.Add(instance);
                }
            }
        }

        public ActorStateResponse[] GetStatesByWatchSet()
        {
            ActorStateResponse[] responses = new ActorStateResponse[watchSets.Count];
            for (int i = 0; i < watchSets.Count; i++)
            {
                responses[i] = new ActorStateResponse()
                {
                    UUID = watchSets[i].UUID,
                    State = watchSets[i].State,
                    Command = watchSets[i].Command,
                    Path = watchSets[i].Path,
                };
            }
            return responses;
        }
    }
}